# @Author：silence
# @Email：792245022@qq.com
# @Date：2025/2/25 2:27
from house_service import *
from utils import *

class HouseView:

    __house_service: HouseService = HouseService()

    def main_menu(self):
        """
        显示主菜单，并让用户选择
        :return:
        """
        while True:
            print()
            print("房屋出租系统菜单".center(50, "="))
            print("\t\t\t1. 新 增 房 源")
            print("\t\t\t2. 查 找 房 屋")
            print("\t\t\t3. 删 除 房 屋 信 息")
            print("\t\t\t4. 修 改 房 屋 信 息")
            print("\t\t\t5. 房 屋 列 表")
            print("\t\t\t6. 退 出")

            key = input("请输入你的选择(1-6)：")
            match key:
                case "1": self.add()
                case "2": self.find_house()
                case "3": self.del_house()
                case "4": self.update_house()
                case "5": self.list_house()
                case "6":
                    if self.exit_sys():
                        break
                case _:
                    print("输入有误，请重新输入")

    def list_house(self):
        print()
        print("房屋列表".center(50, "="))
        print("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)")
        for house in self.__house_service.get_house_list():
            print(f"{house.id}\t\t{house.name}\t\t{house.phone}\t\t{house.address}\t\t{house.rent}\t\t{house.status}")
        print("房屋列表显示完毕".center(50, "="))

    def add(self):
        print("添加房屋".center(50, "="))
        name = input("姓名：")
        phone = input("电话：")
        address = input("地址：")
        rent = int(input("月租："))
        status = input("状态：")

        house = House(
            id=max([house.id for house in self.__house_service.get_house_list()]) + 1,
            name=name,
            phone=phone,
            address=address,
            rent=rent,
            status=status
        )
        self.__house_service.add_house(house)
        print("添加房屋成功".center(50, "="))

    def del_house(self):
        """
        删除房屋的界面
        :return:
        """
        print("删除房屋信息".center(50, "="))
        num = int(input("请输入要删除的房屋编号（-1退出）："))

        if num == -1:
            print("放弃删除房屋信息".center(50, "="))
            return

        if HouseUtil.read_confirm_select().upper() == "Y":
            # 根据 del_id 去 hoses 列表查找是否存在该房屋
            del_flag = self.__house_service.del_by_id(num)
            if del_flag:
                print("删除房屋信息成功".center(50, "="))
            else:
                print("房屋编号不存在，删除失败".center(50, "="))
        else:
            print("放弃删除房屋信息".center(50, "="))

    def exit_sys(self):
        """
        退出系统
        :return:
        """
        return True if HouseUtil.read_confirm_select().upper() == "Y" else False

    def find_house(self):
        """
        查找房屋信息
        :return:
        """
        print("查找房屋信息".center(50, "="))
        num = int(input("请输入要查找的房屋编号（-1退出）："))
        if num == -1:
            print("放弃查找房屋信息".center(50, "="))
            return
        house = self.__house_service.find_by_id(num)
        if house:
            print("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)")
            print(f"{house.id}\t\t{house.name}\t\t{house.phone}\t\t{house.address}\t\t{house.rent}\t\t{house.status}")
            print("查找房屋信息成功".center(50, "="))
        else:
            print("房屋编号不存在，查找失败".center(50, "="))

    def update_house(self):
        """
        更新房屋信息
        :return:
        """
        print("更新房屋信息".center(50, "="))
        num = int(input("请输入要更新的房屋编号（-1退出）："))
        if num == -1:
            print("放弃更新房屋信息".center(50, "="))
            return
        house = self.__house_service.find_by_id(num)
        if not house:
            print("房屋编号不存在，更新失败".center(50, "="))
            return
        house.name = input("姓名：")
        house.phone = input("电话：")
        house.address = input("地址：")
        house.rent = int(input("月租："))
        house.status = input("状态：")
        print("更新房屋信息成功".center(50, "="))